﻿using ConfExpert.Forms.ImportWizards;
using DevTeam;


using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.IT
{
    public partial class ImportAtil : XImportWizard
    {
        public ImportAtil()
        {
            InitializeComponent();
            gleTL.Edit.Assign(RI(Rp.V_User));
            gleTL.EditValueChanged += gleTL_EditValueChanged;

            Init("ImportAtil", false);
            this.FileDone += ImportAtil_FileDone;
            this.ProcessDone += NewOrder_ProcessDone;
            this.ProcessStart += NewOrder_ProcessStart;

            btnSave.Enabled = false;
            btnSave.ItemClick += BtnSave_ItemClick;
            splitter.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
        }

        void gleTL_EditValueChanged(object sender, EventArgs e)
        {
            Enbl();
        }

        void Enbl()
        {
            btnProcessFile.Enabled = gleTL.EditValue.ToInt() > 0;
        }

        Dictionary<string, int> indexes;
        void ImportAtil_FileDone(DataSet Data, string FileName)
        {
            dtAbb.Clear();
            indexes = new Dictionary<string, int>();
            foreach (DataTable dt in Data.Tables)
            {
                string article = dt.TableName;
                //if (!sheetName.Contains("FATURALAR"))
                //    continue;

                int basliksatiri = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow r = dt.Rows[i];
                    if (r[0].ToStr() == "Customer Id")
                    {
                        basliksatiri = i;
                        return;
                    }
                }

                DataRow dr0 = dt.Rows[basliksatiri];
                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                    switch (dr0[colIndex].ToString())
                    {
                        case "Customer":
                        case "Customer Id": indexes.Add("Customer", colIndex); break;
                        case "Artikel":
                        case "Product Id": indexes.Add("Product", colIndex); break;
                        case "Qty": indexes.Add("Qty", colIndex); break;
                        case "Kalis Orani": indexes.Add("KalisOrani", colIndex); break;
                        case "Yas": indexes.Add("Yas", colIndex); break;
                        case "Açıklama-Yorum":
                        case "Aciklama": indexes.Add("Aciklama", colIndex); break;
                        case "Mal Sahibi": indexes.Add("Owner", colIndex); break;
                        case "Takım Lideri Adı":
                        case "Team Leader Id": indexes.Add("TeamLeader", colIndex); break;
                        case "Siparişi Veren Kim( Karbel mi-Müşteri mi)": indexes.Add("SiparisiVeren", colIndex); break;
                        case "SEBEBİ": indexes.Add("Sebebi", colIndex); break;
                        case "Fatura Edilecek mi Edilmeyecek mi": indexes.Add("FaturaEdilecekMi", colIndex); break;
                        case "SON DURUM": indexes.Add("SonDurum", colIndex); break;
                    }


                for (int i = basliksatiri + 1; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];

                    if (dr[indexes["TeamLeader"]] != DBNull.Value)
                    //if (dr[indexes["TeamLeader"]].ToStr() == txtDosyadaki.EditValue.ToStr())
                    {
                        DataRow drAbb = dtAbb.NewRow();
                        foreach (KeyValuePair<string, int> ind in indexes)
                            drAbb[ind.Key] = dr[ind.Value];
                        dtAbb.Rows.Add(drAbb);
                    }
                }
            }

        }


        DataTable dtAbb;
        void NewOrder_ProcessStart()
        {
            dtAbb = new DataTable();
            dtAbb.Columns.Add("Customer", typeof(string));
            dtAbb.Columns.Add("Product", typeof(string));
            dtAbb.Columns.Add("Qty", typeof(decimal));
            dtAbb.Columns.Add("KalisOrani", typeof(decimal));
            dtAbb.Columns.Add("Yas", typeof(decimal));
            dtAbb.Columns.Add("Aciklama", typeof(string));
            dtAbb.Columns.Add("Owner", typeof(string));
            dtAbb.Columns.Add("TeamLeader", typeof(string));
            dtAbb.Columns.Add("SiparisiVeren", typeof(string));
            dtAbb.Columns.Add("Sebebi", typeof(string));
            dtAbb.Columns.Add("FaturaEdilecekMi", typeof(string));
            dtAbb.Columns.Add("SonDurum", typeof(string));
        }


        string xmla;
        //DataTable t;
        void NewOrder_ProcessDone()
        {
            xmla = dtAbb.Serialize();

            DataSet d = ExecuteDataSet("GetAtilImport", xmla, gleTL.EditValue.ToInt());

            MasterGrid.Init(d.Tables[0]);
            MasterGrid.AddSummary();
            gvMaster.BestFitColumns();

            //if (d.Tables[1].Rows.Count > 0 ||
            //    d.Tables[2].Rows.Count > 0)
            //{
            //    btnSave.Enabled = false;

            //    string srkt = string.Empty;
            //    foreach (DataRow r in d.Tables[1].Rows)
            //        srkt += r["Sirket"] + Environment.NewLine;

            //    string tip = string.Empty;
            //    foreach (DataRow r in d.Tables[2].Rows)
            //        tip += r["IT_FaturaTipi"] + Environment.NewLine;

            //    ShowErrorMessage("Aktarımdan önce aşağıdaki değerlerin veritabanına girilmesi gerekiyor!" + Environment.NewLine + Environment.NewLine + srkt + Environment.NewLine + tip);
            //}
            //else
            //    btnSave.Enabled = true;
        }


        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //gvMaster.EndEdit();

            //ExecuteScalar("IT_UpdAtilImport", MasterGrid.Serialize());

            //Notify("Aktarıldı.");
            //btnSave.Enabled = false;

        }

        private void ImportAtil_Load(object sender, EventArgs e)
        {

        }


    }
}